package com.sunkingdom.qq.user.controller.impl;

import com.sunkingdom.qq.global.Result;
import com.sunkingdom.qq.user.controller.LoginController;
import com.sunkingdom.qq.user.entity.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class LoginControllerImpl implements LoginController {
	private static final String LOGIN_PAGE = "/user/user-login";
	

	@Autowired
	User user;

	@RequestMapping(value = "/user", method = RequestMethod.POST)
	public String validateUser(HttpServletRequest request,
			HttpServletResponse response, HttpSession session) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		if ("test".equals(username) && "test".equals(password)) {
			user.setUsername(username);
			user.setPassword(password);
			session.setAttribute("user", user);
			request.setAttribute(Result.MESSAGE, Result.LOGIN_SUCCESS);
		} else {
			request.setAttribute(Result.MESSAGE, Result.LOGIN_FAIL);
		}
		return Result.RESULT;
	}

	@RequestMapping(value = "/user", method = RequestMethod.GET)
	public String validateUserPage() {
		return LOGIN_PAGE;
	}

	
}
